

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.*;

/**
 * @author 绛河
 * 管理员功能页
 */
public class FunctionAdministrator {
    DatabaseConnection dc = new DatabaseConnection();
    Font font1 = new Font("宋体", Font.BOLD, 20);
    Font font = new Font("仿宋", Font.BOLD, 20);
    JFrame page1;
    JMenuBar menuBar;
    ImageIcon imageIcon1 = new ImageIcon("src\\practicaltraining\\1.png");
    ImageIcon imageIcon2 = new ImageIcon("src\\practicaltrainingc\\2.png");
    ImageIcon imageIcon3 = new ImageIcon("src\\practicaltraining\\3.png");
    ImageIcon imageIcon4 = new ImageIcon("src\\practicaltraining\\4.png");
    ImageIcon imageIcon5 = new ImageIcon("src\\practicaltraining\\6.png");
    ImageIcon imageIcon6 = new ImageIcon("src\\practicaltraining\\7.png");
    ImageIcon imageIcon7 = new ImageIcon("src\\practicaltraining\\9.png");
    JLabel textIcon1 = new JLabel(imageIcon1);
    JLabel textIcon2 = new JLabel(imageIcon2);
    JLabel textIcon3 = new JLabel(imageIcon3);
    JLabel textIcon4 = new JLabel(imageIcon4);
    JLabel textIcon5 = new JLabel(imageIcon5);
    JLabel textIcon6 = new JLabel(imageIcon6);
    JLabel textIcon7 = new JLabel(imageIcon7);

    public void administrator() {
        page1 = new JFrame("管理员功能页");
        page1.setSize(700, 550);
        page1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        page1.setLocationRelativeTo(null);
        page1.setLayout(null);
        menuBar = new JMenuBar();
        JMenu m1 = new JMenu("课程管理");
        JMenu m2 = new JMenu("班级管理");
        JMenuItem mi1 = new JMenuItem("录入课程");
        JMenuItem mi2 = new JMenuItem("增添班级");
        JMenuItem mi3 = new JMenuItem("班级课程");
        m1.add(mi1);
        m2.add(mi2);
        m2.add(mi3);

        m1.setFont(font1);
        m2.setFont(font1);
        mi1.setFont(font1);
        mi2.setFont(font1);
        mi3.setFont(font1);
        menuBar.add(m1);
        menuBar.add(m2);
        menuBar.setBounds(0, 0, 600, 80);
        mi1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addTheCourse();
            }
        });
        mi2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addTheClass();
            }
        });
        mi3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                CurriculaVariable();
            }
        });
        textIcon1.setBounds(40, 70, 130, 120);
        textIcon2.setBounds(200, 70, 130, 120);
        textIcon3.setBounds(360, 70, 130, 120);
        textIcon4.setBounds(520, 70, 130, 120);
        textIcon5.setBounds(60, 220, 140, 130);
        textIcon6.setBounds(270, 220, 140, 130);
        textIcon7.setBounds(470, 220, 140, 130);
        page1.setJMenuBar(menuBar);
        page1.add(textIcon1);
        page1.add(textIcon2);
        page1.add(textIcon3);
        page1.add(textIcon4);
        page1.add(textIcon5);
        page1.add(textIcon6);
        page1.add(textIcon7);
        page1.setVisible(true);
    }

    JFrame page2;
    JLabel textIcon8, textIcon9;
    JTextField entryField1, entryField2;
    JButton button1;

    public void addTheClass() {
        page2 = new JFrame("增加班级");
        page2.setSize(650, 400);
        page2.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        page2.setLocationRelativeTo(null);
        page2.setLayout(null);
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        textIcon8 = new JLabel("班级号:");
        textIcon8.setBounds(160, 80, 120, 40);
        textIcon8.setFont(font);
        entryField1 = new JTextField(null, 20);
        entryField1.setBounds(280, 80, 180, 40);
        entryField1.setFont(font);
        textIcon9 = new JLabel("班级名:");
        textIcon9.setBounds(160, 160, 120, 40);
        textIcon9.setFont(font);
        entryField2 = new JTextField(null, 20);
        entryField2.setBounds(280, 160, 180, 40);
        entryField2.setFont(font);
        button1 = new JButton("保存");
        button1.setBounds(280, 240, 100, 40);
        button1.setFont(font);
        button1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                PreparedStatement st = null, st1 = null, st2 = null;
                ResultSet rs = null, rs2 = null;
                Connection con = null;
                //注册驱动
                try {
                    con = dc.getConnection();
                    String sql = "insert into class(class_number,class_name) values(?,?)";
                    String sql2 = "select * from class";
                    st = con.prepareStatement(sql);
                    st1 = con.prepareStatement(sql2);
                    rs = st1.executeQuery();
                    if (entryField1.getText().length() != 0) {
                        st.setString(1, entryField1.getText());
                    } else {
                        JOptionPane.showMessageDialog(null, "班级号不能为空！");
                    }
                    if (entryField2.getText().length() != 0) {
                        st.setString(2, entryField2.getText());
                    } else {
                        JOptionPane.showMessageDialog(null, "班级名不能为空！");
                    }
                    boolean flag = false;
                    while (rs.next()) {
                        if (rs.getString(1).equals(entryField1.getText())) {
                            flag = true;
                        }
                    }
                    if (flag) {
                        JOptionPane.showMessageDialog(null, "该班级已经存在！");
                        entryField1.setText(null);
                        entryField2.setText(null);
                        con.close();
                    }

                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
                if (entryField1.getText().length() != 0 && entryField2.getText().length() != 0) {
                    JOptionPane.showMessageDialog(null, "添加成功！");
                    entryField1.setText(null);
                    entryField2.setText(null);
                    try {
                        st.executeUpdate();
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });

        page2.add(textIcon8);
        page2.add(entryField1);
        page2.add(textIcon9);
        page2.add(entryField2);
        page2.add(button1);
        page2.setVisible(true);
    }

    JFrame page3;
    JLabel textIcon10, textIcon11;
    JTextField entryField3, entryField4;
    JButton button2;

    public void addTheCourse() {
        page3 = new JFrame("添加课程");
        page3.setSize(550, 400);//设置窗体大小
        page3.setLocationRelativeTo(null);//设置窗口每次启动都显示在屏幕正中央
        page3.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        page3.setLayout(null);
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        textIcon10 = new JLabel("课程号：");
        textIcon10.setBounds(110, 100, 100, 40);
        textIcon10.setFont(font);
        entryField3 = new JTextField(null, 20);
        entryField3.setBounds(200, 100, 180, 40);
        entryField3.setFont(font);
        textIcon11 = new JLabel("课程名：");
        textIcon11.setBounds(110, 180, 100, 40);
        textIcon11.setFont(font);
        entryField4 = new JTextField(null, 20);
        entryField4.setBounds(200, 180, 180, 40);
        entryField4.setFont(font);
        button2 = new JButton("添加");
        button2.setBounds(210, 260, 80, 40);
        button2.setFont(font);
        button2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                PreparedStatement ps, ps1;//PreparedStatement==>表示预编译的SQL语句的对象。
                // SQL语句已预编译并存储在PreparedStatement对象中。
                Connection con;//Connection==>与特定数据库的连接
                ResultSet rs = null;
                try {
                    con = dc.getConnection();
                    //执行SQL语句
                    String sql = "insert into course(course_number,course_name) values(?,?)";
                    String sql1 = "select * from course";
                    ps = con.prepareStatement(sql);
                    ps1 = con.prepareStatement(sql1);
                    if (entryField3.getText().length() != 0) {
                        //注：这里的所有getText()都不能写成!=null
                        ps.setString(1, entryField3.getText());
                    } else {
                        JOptionPane.showMessageDialog(null, "课程号不能为空！");//弹出窗口
                    }
                    if (entryField4.getText().length() != 0) {
                        ps.setString(2, entryField4.getText());
                    } else {
                        JOptionPane.showMessageDialog(null, "课程名不能为空！");
                    }
                    rs = ps1.executeQuery();
                    boolean flag = false;
                    while (rs.next()) {
                        if (entryField3.getText().equals(rs.getString(1))) {
                            flag = true;
                        }
                    }
                    if (flag) {
                        JOptionPane.showMessageDialog(null, "课程已经存在！");
                        entryField3.setText(null);
                        entryField4.setText(null);
                        con.close();
                    }
                    if (entryField3.getText().length() != 0 && entryField4.getText().length() != 0) {
                        JOptionPane.showMessageDialog(null, "添加成功！");
                        entryField3.setText(null);
                        entryField4.setText(null);
                        try {
                            ps.executeUpdate();//将数据存储到数据库中
                        } catch (SQLException ex) {
                            ex.printStackTrace();
                        }
                    }
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        });
        //将这些按钮和文本等加入到窗体中
        page3.add(entryField3);
        page3.add(textIcon11);
        page3.add(textIcon10);
        page3.add(entryField4);
        page3.add(button2);
        page3.setVisible(true);//让组件显示
    }

    JFrame page4;
    JLabel textIcon12, textIcon13, textIcon14, textIcon15, textIcon16, textIcon17;
    JTextField entryField5, entryField6, entryField7, entryField8, entryField9, entryField10;
    JButton button3;

    public void CurriculaVariable() {
        page4 = new JFrame("选课页面");
        page4.setSize(600, 550);//设置窗体大小
        page4.setLocationRelativeTo(null);//设置窗口每次启动都显示在屏幕正中央
        page4.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        page4.setLayout(null);
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        textIcon12 = new JLabel("班级号:");
        textIcon12.setBounds(130, 40, 100, 40);
        textIcon12.setFont(font);
        entryField5 = new JTextField(null, 20);
        entryField5.setBounds(240, 40, 180, 40);
        entryField5.setFont(font);
        textIcon13 = new JLabel("班级名:");
        textIcon13.setBounds(130, 100, 100, 40);
        textIcon13.setFont(font);
        entryField6 = new JTextField(null, 20);
        entryField6.setBounds(240, 100, 180, 40);
        entryField6.setFont(font);
        textIcon14 = new JLabel("课程号:");
        textIcon14.setBounds(130, 160, 100, 40);
        textIcon14.setFont(font);
        entryField7 = new JTextField(null, 20);
        entryField7.setBounds(240, 160, 180, 40);
        entryField7.setFont(font);
        textIcon15 = new JLabel("课程名:");
        textIcon15.setBounds(130, 220, 100, 40);
        textIcon15.setFont(font);
        entryField8 = new JTextField(null, 20);
        entryField8.setBounds(240, 220, 180, 40);
        entryField8.setFont(font);
        textIcon16 = new JLabel("教师号:");
        textIcon16.setBounds(130, 280, 100, 40);
        textIcon16.setFont(font);
        entryField9 = new JTextField(null, 20);
        entryField9.setBounds(240, 280, 180, 40);
        entryField9.setFont(font);
        textIcon17 = new JLabel("教师名:");
        textIcon17.setBounds(130, 340, 100, 40);
        textIcon17.setFont(font);
        entryField10 = new JTextField(null, 20);
        entryField10.setBounds(240, 340, 180, 40);
        entryField10.setFont(font);
        button3 = new JButton("保存");
        button3.setBounds(260, 400, 80, 40);
        button3.setFont(font);
        button3.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                PreparedStatement st = null, st1 = null, st2 = null, st3 = null;
                ResultSet rs = null, rs2 = null, rs3 = null;
                Connection con = null;
                //注册驱动
                try {
                    con = dc.getConnection();
                    String sql = "insert into course_arrangement(class_number,class_name,course_number,course_name,teacher_number,teacher_name) values(?,?,?,?,?,?)";
                    String sql2 = "select * from class";
                    String sql3 = "select * from course";
                    String sql4 = "select * from teacher_message";
                    st = con.prepareStatement(sql);
                    st1 = con.prepareStatement(sql2);
                    st2 = con.prepareStatement(sql3);
                    st3 = con.prepareStatement(sql4);
                    rs = st1.executeQuery();
                    boolean flag1 = false;
                    while (rs.next()) {
                        if (rs.getString(1).equals(entryField5.getText())) {
                            flag1 = true;
                        }
                    }
                    rs2 = st2.executeQuery();
                    boolean flag2 = false;
                    while (rs2.next()) {
                        if (rs2.getString(1).equals(entryField7.getText())) {
                            flag2 = true;
                        }
                    }
                    rs3 = st3.executeQuery();
                    boolean flag3 = false;
                    while (rs3.next()) {
                        if (rs3.getString(1).equals(entryField9.getText())) {
                            if (rs3.getString(3).equals(entryField8.getText())) {
                                flag3 = true;
                            }

                        }
                    }
                    if (flag1 && flag2 && flag3) {
                        st.setString(1, entryField5.getText());
                        st.setString(2, entryField6.getText());
                        st.setString(3, entryField7.getText());
                        st.setString(4, entryField8.getText());
                        st.setString(5, entryField9.getText());
                        st.setString(6, entryField10.getText());
                        st.executeUpdate();
                        JOptionPane.showMessageDialog(null, "添加成功");

                    } else {
                        JOptionPane.showMessageDialog(null, "添加失败");
                    }
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        });
        page4.add(textIcon12);
        page4.add(entryField5);
        page4.add(textIcon13);
        page4.add(entryField6);
        page4.add(textIcon14);
        page4.add(entryField7);
        page4.add(textIcon15);
        page4.add(entryField8);
        page4.add(textIcon16);
        page4.add(entryField9);
        page4.add(textIcon17);
        page4.add(entryField10);
        page4.add(button3);
        page4.setVisible(true);
    }

}
